package com.zzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.entity.User;
import com.zzy.entity.UserRelation;
import com.zzy.mapper.UserMapper;
import com.zzy.mapper.UserRelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRelationMapper userRelationMapper;
    @GetMapping("/")
    public String welcome(){
        return "login";
    }


    @PostMapping("/login")
    public String login(User user, HttpServletRequest request, Model model){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 != null){
            //更新在线
            model.addAttribute("user",user1);
            user1.setIsLogin(1);
            userMapper.updateById(user1);
            //设置session
            HttpSession session = request.getSession();
            session.setAttribute("user",user1);
            //得到数据返回给页面
            model.addAttribute("userFriends",getFriends(getAllUserRelationByUId(user1.getUId())));
            return "index";
        }else {
            model.addAttribute("loginMes","用户名或密码错误");
            return "login";
        }
    }
    @PostMapping("/register")
    public String register(User user,Model model){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 != null){
            model.addAttribute("registerMes","用户已存在");
            return "login";
        }
        userMapper.insert(user);
        model.addAttribute("registerMes","用户注册成功");
        return "login";
    }

    @PostMapping("/exit")
    public String exit(HttpServletRequest request, Model model){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        user.setIsLogin(0);

        userMapper.updateById(user);

        session.removeAttribute("user");
        return "login";
    }

    public List<UserRelation> getAllUserRelationByUId(Integer uId){
        QueryWrapper<UserRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id",uId);
        List<UserRelation> userRelations = userRelationMapper.selectList(wrapper);
        return userRelations;
    }

    @GetMapping("/addFriend/{fId}")
    public String addFriend(@PathVariable("fId") Integer fId){
        System.out.println(fId);

        return "";
    }
    public List<User> getFriends(List<UserRelation> list){
        List<User> userFriends = new ArrayList<>();
        for (UserRelation userRelation : list) {
            User user = userMapper.selectById(userRelation.getFId());
            userFriends.add(user);
        }
        return userFriends;
    }
}
